package com.kly.order.db.dao.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Maps;
import com.kly.order.db.dao.ProductDiscountConfigDao;
import com.kly.order.db.entity.ProductDiscountConfigEntity;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * User: user
 * Date: 2024-01-25
 * Time: 14:46:14.018
 */

@Repository
public class ProductDiscountConfigDaoImpl extends AbstractBaseMapper<ProductDiscountConfigEntity> implements ProductDiscountConfigDao {

    @Override
    public ProductDiscountConfigEntity productDiscountCountByPromotionType(Integer promotionType, String appCode, Integer memberType) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("promotionType", promotionType);
        params.put("appCode", appCode);
        params.put("memberType", memberType);
        return this.getSqlSession().selectOne(this.getStatement("productDiscountCountByPromotionType"), params);
    }

    @Override
    public List<ProductDiscountConfigEntity> listDiscount(Integer promotionType, Integer memberType, String appCode, Page page) {
        return this.getSqlSession().selectList(this.getStatement("listDiscount"), setupParams(promotionType, memberType, appCode, page));
    }

    @Override
    public Integer getDiscountCnt(Integer promotionType, Integer memberType, String appCode) {
        return this.getSqlSession().selectOne(this.getStatement("getDiscountCnt"), setupParams(promotionType, memberType, appCode, null));
    }

    @Override
    public ProductDiscountConfigEntity getDiscountById(Integer id) {
        Map<String, Object> params = Maps.newHashMap();

        if (Objects.nonNull(id)) {
            params.put("id", id);
        }
        return this.getSqlSession().selectOne(this.getStatement("getDiscountById"), params);
    }

    @Override
    public int removeDiscount(Integer id) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", id);
        return this.getSqlSession().delete(this.getStatement("removeDiscount"), params);
    }

    @Override
    public int addDiscount(ProductDiscountConfigEntity entity) {
        return this.getSqlSession().insert(this.getStatement("addDiscount"), entity);
    }


    private Map<String, Object> setupParams(Integer promotionType, Integer memberType, String appCode, Page page) {
        Map<String, Object> params = Maps.newHashMap();
        if (Objects.nonNull(promotionType) && promotionType != 99) {
            params.put("promotionType", promotionType);
        }
        if (Objects.nonNull(memberType) && memberType != 99) {
            params.put("memberType", memberType);
        }
        if (StringUtils.isNotEmpty(appCode)) {
            params.put("appCode", appCode);
        }
        if (page != null) {
            params.put("page", page);
        }
        return params;
    }

    @Override
    public List<ProductDiscountConfigEntity> productDiscountByPromotionType(Integer promotionType, String appCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("promotionType", promotionType);
        params.put("appCode", appCode);
        return this.getSqlSession().selectList(this.getStatement("productDiscountByPromotionType"), params);
    }
}